import fs from 'fs';

const path = require('path');

const rootPathy = path.resolve(__dirname, '../');
const envConfigFile = rootPathy + '/configure/currentEnv.js';

(async () => {
  // 更新环境配置缓存
  const envConfig = (env) => {
    try {
      fs.writeFileSync(
        envConfigFile,
        '/* eslint-disable import/no-commonjs */\nmodule.exports=' +
          JSON.stringify(
            {
              ...env,
              updatedAt: new Date().toISOString(),
            },
            null,
            2,
          ),
      );
      console.log('env配置文件 -- ok');
    } catch (error) {
      console.log('env配置文件 -- fail');
    }
  };

  const argvEnv = {
    customeColor: '[customeColor]',
  };

  process.argv.forEach((arg) => {
    const [key, value] = arg.split('=');
    if (['project','projectName', 'customeColor', 'env', 'platform', 'release'].includes(key) && value) {
      argvEnv[key] = value;
    }
  });

  const prompts = require('prompts');
  let envTmp;
  if (Object.keys(argvEnv).length === 6) {
    envTmp = argvEnv;
    envConfig(argvEnv);
  } else {
    const responseProject = await prompts({
      type: 'select',
      name: 'value',
      message: '选择项目',
      choices: [
        {
          title: '保利',
          description: '',
          value: 'cinema-保利',
        },
        {
          title: '影竞联盟',
          description: '',
          value: 'yingjing-影竞联盟',
        },
        {
          title: '苏宁影城',
          description: '',
          value: 'suning-苏宁影城',
        },
      ],
      initial: 0,
    });

    const responseEnv = await prompts({
      type: 'select',
      name: 'value',
      message: '选择编译环境',
      choices: [
        {
          title: '开发环境',
          description: '',
          value: 'sit',
        },
        {
          title: '线上环境',
          description: '',
          value: 'pro',
        },
      ],
      initial: 0,
    });

    const responsePlatform = await prompts({
      type: 'select',
      name: 'value',
      message: '选择平台',
      choices: [
        {
          title: 'h5',
          description: '',
          value: 'h5',
        },
        {
          title: '微信小程序',
          description: '',
          value: 'weapp',
        },
        {
          title: '抖音小程序',
          description: '',
          value: 'tt',
        },
        {
          title: '支付宝小程序',
          description: '',
          value: 'alipay',
        },
      ],
      initial: 1,
    });

    const responseRelease = await prompts({
      type: 'select',
      name: 'value',
      message: '是否发版',
      choices: [
        {
          title: '是',
          description: '',
          value: 'yes',
        },
        {
          title: '否',
          description: '',
          value: 'no',
        },
      ],
      initial: 0,
    });
    const data = {
      env: responseEnv.value,
      project: responseProject.value.split('-')[0],
      projectName: responseProject.value.split('-')[1],
      platform: responsePlatform.value,
      release: responseRelease.value,
      customeColor: '[customeColor]',
    };

    envTmp = data;
    envConfig(data);
  }
  const navBarStyle = await require('./getAppConfig').getNavBarStyle();
  envConfig({
    ...envTmp,
    navBarStyle,
    navigationBarTextColor: navBarStyle.navigationBarTextStyle,
  });

  // require('./replace.js');
})();
